package com.helloworld.chat.service.impl;

import com.google.gson.Gson;
import com.helloworld.chat.dao.FriendDao;
import com.helloworld.chat.dao.FriendRequestDao;
import com.helloworld.chat.dao.GroupDao;
import com.helloworld.chat.dao.UserDao;
import com.helloworld.chat.entity.Friend;
import com.helloworld.chat.entity.FriendRequest;
import com.helloworld.chat.entity.Group;
import com.helloworld.chat.entity.User;
import com.helloworld.chat.service.FriendService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by xdzy on 2017/6/29.
 */
@Service
@Transactional
public class FriendServiceImpl implements FriendService{
    @Autowired
    private UserDao userDao;
    @Autowired
    private FriendRequestDao friendRequestDao;
    @Autowired
    private GroupDao groupDao;
    @Autowired
    private FriendDao friendDao;
    private static final Logger logger=Logger.getLogger(FriendServiceImpl.class);

    public FriendServiceImpl() {
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void setFriendRequestDao(FriendRequestDao friendRequestDao) {
        this.friendRequestDao = friendRequestDao;
    }

    public void setGroupDao(GroupDao groupDao) {
        this.groupDao = groupDao;
    }

    public void setFriendDao(FriendDao friendDao) {
        this.friendDao = friendDao;
    }

    @Transactional
    @Override
    public String addFriend(FriendRequest friendRequest, User me, String friendName) {
        if(me.getUsername().equals(friendName)){
            return "自己和自己已经是好基友了，不需要添加哦";
        }
        User other=userDao.getUser(friendName);
        if(other==null){
            return "该用户不存在";
        }
        if(friendRequestDao.getFriendRequest(me.getUid(),other.getUid())!=null){
            return "申请已经发送，请不要重复提交";
        }
        if(friendDao.getFriend(me.getUid(),other.getUid())!=null){
            return "好友已存在，请不要重复添加";
        }
        friendRequest.setSender(me);
        friendRequest.setReceiver(other);
        friendRequestDao.addFriendRequest(friendRequest);
        System.out.println("service:"+friendRequest.getGroup().getGid());
        return "申请已发送，请等待对方审核";
    }

    @Override
    public Friend getFriend(int id) {

        return friendDao.getFriend(id);
    }

    @Override
    public List<Friend> getFriendList(int groupId) {
        return null;
    }

    @Override
    public String getFriendList_JSON(int groupId) {
        List<Friend> friends=friendDao.getFriendsInGroup(groupId);
        return new Gson().toJson(friends);
    }

    @Override
    public void update(String alias, int id) {
        Friend friend=friendDao.getFriend(id);
        friend.setAlias(alias);
        friendDao.update(friend);
    }

    @Override
    public void delete(int id) {
        Friend friend=friendDao.getFriend(id);
        Friend friend2=friendDao.getFriend(friend.getFu().getUid(),friend.getUid());
        friendDao.delete(friend);
        friendDao.delete(friend2);
    }
}
